package com.juc.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Author Zan
 * @Create 2024/5/6 22:33
 * @ClassName: CompletableFutureAPIDemo
 * @Description : TODO 请用一句话描述该类的功能
 */
public class CompletableFutureAPIDemo {
    public static void main(String[] args) throws InterruptedException {
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "abc";
        });

//        System.out.println(completableFuture.get());
//        System.out.println(completableFuture.get(2L, TimeUnit.SECONDS));
//        System.out.println(completableFuture.join());
        TimeUnit.SECONDS.sleep(1);
//        System.out.println(completableFuture.getNow("xxx"));

        // complete如果completableFuture没有被打断，那就返回false并且返回返回的值
        //         如果completableFuture被打断了，那就返回true并且返回括号里面我们的传参
        System.out.println((completableFuture.complete("completeValue")) + "\t" + completableFuture.join());
    }
}
